package com.weixing.mall.provider.controller.front;

import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.*;
import com.weixing.mall.provider.api.model.vo.SysDictVo;
import com.weixing.mall.provider.model.domain.User;
import com.weixing.mall.provider.model.vo.FrontUserVo;
import com.weixing.mall.provider.model.vo.sys.SysUserRoleVo;
import com.weixing.mall.provider.service.IUserService;
import com.weixing.mall.push.CidAlias;
import com.weixing.mall.push.GtFwComponent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ceshi
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2020/1/1715:11
 */
@RestController
@RequestMapping("front/user")
@Api(value = "前端后台用户api",tags = "前端:后台用户")
public class FrontUserController  extends BaseController {

    @Autowired
    private IUserService sysUserService;
    @Autowired
    private GtFwComponent gtFwComponent;

    /**
     * 前端后台用户登录后，查询用户角色信息，通过角色信息进行判定跳转的页面
     * @return
     */
    @ApiOperation(value = "根据Id号查询用户信息")
    @GetMapping("querySysUser")
    public Result<FrontUserVo> querySysUser(@SysInfo SysAuth auth) {
       FrontUserVo vo= sysUserService.querySysUser(auth.getUid());
        return  ResultUtil.success(vo);
    }

    /**
     * 前端会员登录要要调用该方法来查询基础的用户信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据Id号查询用户信息")
    @GetMapping("queryMemberUser")
    public Result<FrontUserVo> queryMemberUser(Long id) {
        FrontUserVo vo= sysUserService.queryMemberUser(id);
        return  ResultUtil.success(vo);
    }

    @ApiOperation(value = "根据角色编码查用户List")
    @GetMapping("/getUserByRoleCode")
    public Result selectUserByRoleCode(String roleCode,Integer isEnable) {
        List<SysUserRoleVo> sysUserRoleVos = sysUserService.selectSysUserByRoleCode(roleCode,isEnable);
        return ResultUtil.success(sysUserRoleVos);
    }



    @ApiOperation(value = "绑定cid")
    @PostMapping("bindCid/{cid}")
    public Result bindCid( @PathVariable String cid) {
        SysAuth sysExt = AuthUtil.getSysExt();
        Long mid = sysExt.getUid();
        User byId = sysUserService.getById(mid);
        sysUserService.updateCid(byId.getMobile(), mid);
        List<CidAlias> list = Lists.newArrayList();
        CidAlias alias = new CidAlias();
        alias.setCid(cid);
        alias.setAlias(byId.getMobile());
        list.add(alias);
        gtFwComponent.bindAlias(list);
        return ResultUtil.success();
    }


}
